package com.emcom.gotour.mvc.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.emcom.gotour.mvc.model.TourContentModel;
import com.emcom.gotour.mvc.view.GlobalView;
import com.emcom.gotour.mvc.view.InsertTourContentView;
import com.emcom.gotour.mvc.view.NavigationView;
import com.emcom.gotour.mvc.view.TourContentView;
import com.emcom.gotour.po.TourContent;
import com.emcom.gotour.service.FileUploadService;
import com.emcom.gotour.service.TourCategoryService;
import com.emcom.gotour.service.TourManageService;
import com.emcom.gotour.utils.LocaleUtils;
import com.emcom.gotour.validation.MultipartFileValidator;

@Controller
public class InsertTourController {

	@Autowired
	private TourManageService tourManageService;

	@Autowired
	private TourCategoryService tourCategoryService;

	@Autowired
	private MessageSource messageSource;

	@Autowired
	private FileUploadService fileUploadService;

	@Autowired
	private MultipartFileValidator multipartFileValidator;

	@RequestMapping(value={"/admin/insert_tour.html"})
	public String insertTour(HttpServletRequest request) {
		InsertTourContentView view = new InsertTourContentView();
		view.setCategoryList(tourCategoryService.queryCategory("ZHOU_BIAN"));
		view.setTitle(messageSource.getMessage("tour.insert.title", null, LocaleUtils.getLocale(request)));
		view.setNavigationView(new NavigationView(request, messageSource));
		view.setGlobalView(new GlobalView(request, messageSource));
		view.setTourContentView(new TourContentView(request, messageSource));
		request.setAttribute("view", view);
		return "/admin/insert_tour";
	}

	@RequestMapping(value={"/admin/insert_tour_action.html"})
	public String insertTourAction(
			@RequestParam(value="adImageFile", required=false) MultipartFile adImageFile,
			@RequestParam(value="imageFile", required=false) MultipartFile imageFile,
			@RequestParam(value="imageFile1", required=false) MultipartFile imageFile1,
			@RequestParam(value="imageFile2", required=false) MultipartFile imageFile2,
			@RequestParam(value="imageFile3", required=false) MultipartFile imageFile3,
			@RequestParam(value="imageFile4", required=false) MultipartFile imageFile4,
			HttpServletRequest request) {
		try {
			if(adImageFile != null) {
				multipartFileValidator.validate(adImageFile);
			}
			if(imageFile != null) {
				multipartFileValidator.validate(imageFile);
			}
			TourContent tourContent = TourContentModel.getObject(request, fileUploadService, adImageFile, imageFile, imageFile1, imageFile2, imageFile3, imageFile4);
			if(tourContent != null) {
				tourManageService.saveTour(tourContent);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return insertTour(request);
	}

}
